package com.sinoglobal.shakeprize.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.shakeprize.R;

/**
 * @author qiwx
 * @createdate 2014-7-4 下午1:18:48
 * @Description: 提示信息
 */
public class ToastBuilder {

	private static Toast toast;
	private static Context mContext = null;
	static ToastBuilder builder;
	private static long oneTime = 0;// 第一次弹出Toast时间
	private static long twoTime = 0;// 第二次弹出Toast时间
	private static String oldMsg;

	public static ToastBuilder getIntance(Context context) {
		mContext = context;
		if (builder == null)
			builder = new ToastBuilder();
		return builder;
	}

	public void showToast(String info, int gravity) {
		toast = new Toast(mContext);
		toast.setGravity(gravity, 0, 0);
		toast.setDuration(800);
		if (null == info)
			return;
		TextView view = new TextView(mContext);
		// view.setCompoundDrawables(null,
		// mContext.getResources().getDrawable(R.drawable.alert_expression),
		// null, null);
		view.setBackgroundColor(mContext.getResources().getColor(R.color.white));
		int width = mContext.getResources().getDisplayMetrics().widthPixels / 2;
		int height = width / 2;
		view.setWidth(width);
		view.setHeight(height);
		int pading = EquipmentUtil.dip2px(mContext, 4);
		view.setPadding(pading, pading, pading, pading);
		view.setTextColor(mContext.getResources().getColor(R.color.black));
		view.setTextSize(18);
		// view.setLayoutParams(new LayoutParams(width, height));
		view.setGravity(Gravity.CENTER);
		view.setText(info);
		toast.setView(view);
		toast.show();
	}

	public void showToast(String info) {
		if (toast == null) {
			toast = Toast.makeText(mContext, info, Toast.LENGTH_SHORT);
			toast.show();
			oneTime = System.currentTimeMillis();
		} else {
			twoTime = System.currentTimeMillis();
			if (info.equals(oldMsg)) {
				if (twoTime - oneTime > Toast.LENGTH_SHORT) {
					toast.show();
				}
			} else {
				oldMsg = info;
				toast.setText(info);
				toast.show();
			}
		}
		oneTime = twoTime;

		// Toast.makeText(mContext, info, Toast.LENGTH_SHORT).show();

		/*
		 * toast = new Toast(mContext); toast.setGravity(Gravity.CENTER, 0, 0);
		 * toast.setDuration(1000); if (null == info) return; TextView view =
		 * new TextView(mContext); // view.setCompoundDrawables(null,
		 * mContext.getResources().getDrawable(R.drawable.alert_expression),
		 * null, null);
		 * view.setBackgroundColor(mContext.getResources().getColor(
		 * R.color.white)); int width = FlyApplication.widthPixels / 2; int
		 * height = width/2; view.setWidth(width); view.setHeight(height); int
		 * pading=EquipmentUtil.dip2px(mContext, 4); view.setPadding(pading,
		 * pading, pading, pading);
		 * view.setTextColor(mContext.getResources().getColor(R.color.black));
		 * view.setTextSize(18); // view.setLayoutParams(new LayoutParams(width,
		 * height)); view.setGravity(Gravity.CENTER); view.setText(info);
		 * toast.setView(view); toast.show();
		 */
	}
}
